<?php
include("sessionUser.php");
include("Conexao.php");

$conexao = new Conexao();

$con = $conexao->conectBD();

// paginacao 
if($_GET["pagina"])
    $pagina = $_GET["pagina"];
else 
    $pagina = 1;
if($_POST["caracteristica"]){ // buscar 
    $x = strtoupper($_POST["caracteristica"]);
    $carac = "%".$x."%";
}
else if ($_GET["caracteristica"]){
    
     $x = strtoupper($_GET["caracteristica"]);
      $carac = "%".$x."%";
}
    else {    // nao foi passada nenhuma caracteristica 
$carac = "%";
}
$sql = "SELECT a.idJogo,b.nome AS usuario,c.* FROM tbRecomendacao_equipe05 a INNER JOIN tbUsuario b ON idUsuario = codigo INNER JOIN
(SELECT a.*,b.nome AS cat,c.nome AS fab,d.nome AS forn,e.nome AS loja,f.nome AS plat,g.nome AS jog,h.nome AS con FROM tbJogo_equipe05 a INNER JOIN tbCategoria_equipe05 b 
ON a.idCategoria=b.id INNER JOIN tbFabricante_equipe05 c ON a.idFabricante=c.id 
INNER JOIN  tbFornecedor d ON a.idFornecedor = d.codigo INNER JOIN tbLoja e ON a.idLoja = e.codigo
INNER JOIN tbPlataforma_equipe05 f ON a.idPlataforma = f.id INNER JOIN tbJogabilidade_equipe05 g ON a.idJogabilidade = g.id
INNER JOIN tbConectividade_equipe05 h ON a.idConectividade = h.id ) c ON a.idJogo = c.id   WHERE a.idAmigo = ".$_SESSION["id"]." AND (b.nome like '$carac' OR c.descricao like '$carac' OR c.cat like '$carac' OR c.fab like '$carac'
OR c.forn like '$carac' OR c.loja like '$carac' OR c.plat like '$carac' OR c.jog like '$carac' OR c.con like '$carac')  ORDER BY a.idJogo";
// faz a busca de acordo com a caracteristica informada pelo usuario tras todos

// paginacao
$npp = 6;
$result = pg_query($sql);
$rows = pg_num_rows($result);

$total = ceil($rows/$npp);
$ant = $pp;
$pp = (($pagina * $npp) - $npp);

$sql = "SELECT a.idJogo,b.nome AS usuario,c.* FROM tbRecomendacao_equipe05 a INNER JOIN tbUsuario b ON idUsuario = codigo INNER JOIN
(SELECT a.*,b.nome AS cat,c.nome AS fab,d.nome AS forn,e.nome AS loja,f.nome AS plat,g.nome AS jog,h.nome AS con FROM tbJogo_equipe05 a INNER JOIN tbCategoria_equipe05 b 
ON a.idCategoria=b.id INNER JOIN tbFabricante_equipe05 c ON a.idFabricante=c.id 
INNER JOIN  tbFornecedor d ON a.idFornecedor = d.codigo INNER JOIN tbLoja e ON a.idLoja = e.codigo
INNER JOIN tbPlataforma_equipe05 f ON a.idPlataforma = f.id INNER JOIN tbJogabilidade_equipe05 g ON a.idJogabilidade = g.id
INNER JOIN tbConectividade_equipe05 h ON a.idConectividade = h.id ) c ON a.idJogo = c.id   WHERE a.idAmigo = ".$_SESSION["id"]." AND (b.nome like '$carac' OR c.descricao like '$carac' OR c.cat like '$carac' OR c.fab like '$carac'
OR c.forn like '$carac' OR c.loja like '$carac' OR c.plat like '$carac' OR c.jog like '$carac' OR c.con like '$carac')  ORDER BY a.idJogo LIMIT ".$npp." OFFSET ".$pp;
// faz a busca de acordo com a caracteristica informada pelo usuario tras somente um determinado intervalo
$result = pg_query($sql);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <LINK REL=StyleSheet HREF="main.css" TYPE="text/css" MEDIA=screen>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>   
    <link rel="stylesheet" href="main.css" />
    </head>
    <body>
        <form name="consultaJogo" action="indexUser.php?link=9" method="POST">
            <table border="0 width="100%">
                <thead>
                    <tr>
                        <th colspan="2" align="center"> Filtro de Caracteristicas </th>
                        
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Pesquisa:</td>
                        <td><input type="text" name="caracteristica" value="" size="100" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Buscar" /></td> 
                        
                    </tr>
                </tbody>
            </table>
        </form>
        <table border="0" width="100%" align="center" style="border: 1px solid #ccc;">
            <thead>
                <tr align="center" style="border: 1px solid #ccc;">
                    <td align="center" colspan="2" ><strong><h3>RECOMENDAÇÕES DE AMIGOS</h3> </strong></td>
                </tr>
                <tr align="center" style="border: 1px solid #ccc;">
                    <td align="center" colspan="2" ><strong><h3>JOGOS</h3> <br> <?echo "Pagina ".$pagina." DE ".$total;?></strong></td>
                </tr>
                
            </thead>
            <tbody>
                <?php while ($colum = pg_fetch_array($result)) { ?>  
                <tr style="border: 1px solid #ccc;">
                    <td>
                        <table border="0" width="100%" height =" 100%">
                          <tbody>
                            <tr>
                                <td width="50%">
                                    <table border="0"  width="100%" height =" " style="border: 1px solid #ccc;">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                       <img src="fotosJogos/Fotos BD/<?echo $colum["imagempath"];?>" width="100" height="140" /> 

                                                    </td>
                                                    <td>
                                                        <table border="0" >
                            <tbody>
                                <tr>
                                    <td align="center"><?echo $colum["nome"];?></td>
                                </tr>
                                <tr>
                                    <td><?echo $colum["descricao"];?></td>
                                </tr>
                                 <tr>
                                    <td><strong>RECOMENDARAM: <?echo $colum["usuario"];
                                    $idJogo = $colum["id"];
                                    $sql2 = "SELECT * FROM tbRecomendacao_equipe05 WHERE idJogo = ".$idJogo." and idAmigo = ".$_SESSION["id"];
                                    $result2 = pg_query($sql2);
                                    
                                    if(pg_num_rows($result2) > 1)
                                        echo " + ".(pg_num_rows($result2)-1)." Amigo(s)";
                                    
                                    ?></strong>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                </td>
             </tr>
             <tr>
                 <td align="center" colspan="2"> <a href="indexUser.php?link=6&AMP;id=<?echo $colum["id"];?>&AMP;pagina=<?echo $_GET["pagina"];?>&AMP;caracteristica=<?echo $x;?>&AMP;volta=9" ><img src="images/Visualizar.png" width="40" height="40" /> <br> VISUALIZAR </a></td>
              </tr>
             </tbody>
             </table>
                                    
       </td>
       <? $colum= pg_fetch_array($result);
       if($colum){
       ?>
       <td>
             <table border="0"  width="100%" height =" " style="border: 1px solid #ccc;">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                       <img src="fotosJogos/Fotos BD/<?echo $colum["imagempath"];?>" width="100" height="140" /> 

                                                    </td>
                                                    <td>
                                                        <table border="0" >
                            <tbody>
                                <tr>
                                    <td align="center"><?echo $colum["nome"];?></td>
                                </tr>
                                <tr>
                                    <td><?echo $colum["descricao"];?></td>
                                </tr>
                                 <tr>
                                    <td><strong>RECOMENDARAM: <?echo $colum["usuario"];
                                    $idJogo = $colum["id"];
                                    $sql2 = "SELECT * FROM tbRecomendacao_equipe05 WHERE idJogo = ".$idJogo." and idAmigo = ".$_SESSION["id"];
                                    $result2 = pg_query($sql2);
                                    
                                    if(pg_num_rows($result2) > 1)
                                        echo " + ".(pg_num_rows($result2)-1)." Amigo(s)";
                                    
                                    ?></strong>
                                    </td>
                                </tr>
                                
                            </tbody>
                        </table>
                </td>
             </tr>
             <tr>
                 <td align="center" colspan="2"> <a href="indexUser.php?link=6&AMP;id=<?echo $colum["id"];?>&AMP;pagina=<?echo $_GET["pagina"];?>&AMP;caracteristica=<?echo $x;?>&AMP;volta=9" ><img src="images/Visualizar.png" width="40" height="40" /> <br> VISUALIZAR </a></td>
                 </tr>
             </tbody>
             </table>       </td>
        </tr>
        </tbody>
        </table>
                  </td>
                </tr>
                </tr>
                 <?php }} $conexao->desconectBD(); ?>
            </tbody>
        </table>
        <br><br>
        <?
        
        if($pagina > 1)
            $prev = "<a href= 'indexUser.php?link=9&pagina=".($pagina - 1)."&caracteristica=".$x."'> <img src= 'images/prev.png' width='25' height='25'/></a>";
        if($pagina < $total)
            $next = "<a href= 'indexUser.php?link=9&pagina=".($pagina + 1)."&caracteristica=".$x."'> <img src= 'images/Next.png' width='25' height='25'/> </a>";
        
        echo $prev. "  ".$next;
        
        ?>
        
    </body>
    
</html>